package com.gw.quotes.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gw.quotes.R;
import com.gw.quotes.entity.business.Goodsclass;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.GoodsListQueryServiceImpl;
import com.gw.quotes.util.ACache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.yxw.view.util.ResFinder;

public class SortAdapter extends BaseAdapter {

	private Context context;
	private int resource;
	private ArrayList<Goodsclass> list;
	private ArrayList<Goodsclass> goodsclasslist;
	private DisplayImageOptions options;
	protected ImageLoader imageLoader;
	private boolean flag;
	private ACache aCache;

	// private FinalBitmap finalBitmap;

	public SortAdapter(Context context, int layoutId,
			ArrayList<Goodsclass> list, boolean flag, ACache aCache) {
		this.aCache = aCache;
		this.context = context;
		this.resource = layoutId;
		this.list = list;
		this.flag = flag;
		this.imageLoader = ImageLoader.getInstance();
		this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		this.options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_empty)
				.showImageForEmptyUri(
						ResFinder.getR(context, "R.drawable.ic_empty"))
				.showImageOnFail(ResFinder.getR(context, "R.drawable.ic_empty"))
				.cacheInMemory(true).cacheOnDisk(true)
				.displayer(new FadeInBitmapDisplayer(500))// 是否图片加载好后渐入的动画时间
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		// finalBitmap = FinalBitmap.create(context);// 初始化FinalBitmap模块
		// finalBitmap.configLoadingImage(R.drawable.ic_empty);
		// finalBitmap.configLoadfailImage(R.drawable.ic_empty);
		// finalBitmap.configBitmapLoadThreadSize(6);
		// finalBitmap.configDiskCachePath(AppInfo.CACHE_SD_PATH);
		// finalBitmap.configDiskCacheSize(100*1024*1024);
		// finalBitmap.configMemoryCachePercent(0.3f);
	}

	@Override
	public int getCount() {
		return list==null?0:this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return list==null?null:this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Goodsclass goodsclass = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(this.context).inflate(
					this.resource, parent, false);
		}
		TextView sort_title = (TextView) convertView
				.findViewById(R.id.sort_title);
		sort_title.setText(goodsclass.getGoodsclassname());
		if (this.flag) {
			ImageView sort_img = (ImageView) convertView
					.findViewById(R.id.sort_img);
			TextView sort_content = (TextView) convertView
					.findViewById(R.id.sort_content);

			// sort_content.setText(sort.getSort_content());
			// System.out.println("imgurl:" + goodsclass.getImgurl());
			this.imageLoader.displayImage(goodsclass.getImgurl(), sort_img,
					this.options);
			if ((goodsclasslist = (ArrayList<Goodsclass>) aCache
					.getAsObject("sortlistlv" + position)) == null) {
				getGoodsClass(goodsclass.getGoodsclassguid(), sort_content,
						position);
			} else {
				String content = "";
				for (int i = 0; i < goodsclasslist.size(); i++) {
					if (i == goodsclasslist.size() - 1)
						content += goodsclasslist.get(i).getGoodsclassname();
					else
						content += goodsclasslist.get(i).getGoodsclassname()
								+ "\t";
				}
				sort_content.setText(content);
			}
			// finalBitmap.display(sort_img,
			// "http://cdn3.iconfinder.com/data/icons/transformers/computer.png");
			// this.imageLoader
			// .displayImage(
			// "http://ww1.sinaimg.cn/mw690/e2b067a1gw1e9kfo5ez84g20780554nu.gif",
			// sort_img, this.options);
			// this.imageLoader.displayImage("http://cdn3.iconfinder.com/data/icons/transformers/computer.png",
			// sort_img,
			// this.options);
			// this.imageLoader.displayImage("http://ww1.sinaimg.cn/mw690/e2b067a1gw1e9kfo5ez84g20780554nu.gif",
			// sort_img,
			// this.options);

		} else {
			ImageView sort_img = (ImageView) convertView
					.findViewById(R.id.sort_img);
			TextView sort_content = (TextView) convertView
					.findViewById(R.id.sort_content);
			sort_img.setVisibility(View.GONE);
			sort_content.setVisibility(View.GONE);
		}
		return convertView;
	}

	private void getGoodsClass(String goodsclassguid, final TextView v,
			final int position) {
		BaseServiceInter httpService = new GoodsListQueryServiceImpl();
		Request request = new Request();
		request.setGoodsclassguid(goodsclassguid);
		Response response = httpService.handler(request,
				new HttpCallBack<Response<ArrayList<Goodsclass>>>() {
					@Override
					public void onFinished(
							Response<ArrayList<Goodsclass>> response) {
						if (response.getResult() == 1) {
							String content = "";
							goodsclasslist = response.getObj();
							aCache.put("sortlistlv" + position, goodsclasslist);
							for (int i = 0; i < goodsclasslist.size(); i++) {
								if (i == response.getObj().size() - 1)
									content += response.getObj().get(i)
											.getGoodsclassname();
								else
									content += response.getObj().get(i)
											.getGoodsclassname()
											+ "\t";
							}
							v.setText(content);
						} else {
							Toast.makeText(context, "请求失败", 1).show();
						}
					}
				});
	}
}
